package com.android.settings.audio.peq.peq.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

import com.android.settings.audio.peq.peq.achart.GraphicalView;
import com.android.settings.audio.peq.peq.achart.chart.AbstractChart;
import com.android.settings.audio.peq.peq.achart.chart.LineChart;
import com.android.settings.audio.peq.peq.achart.chart.PointStyle;
import com.android.settings.audio.peq.peq.achart.chart.XYChart;
import com.android.settings.audio.peq.peq.achart.model.TimeSeries;
import com.android.settings.audio.peq.peq.achart.model.XYMultipleSeriesDataset;
import com.android.settings.audio.peq.peq.achart.model.XYSeries;
import com.android.settings.audio.peq.peq.achart.renderer.BasicStroke;
import com.android.settings.audio.peq.peq.achart.renderer.XYMultipleSeriesRenderer;
import com.android.settings.audio.peq.peq.achart.renderer.XYSeriesRenderer;
import com.android.settings.audio.peq.util.BuildConfig;

import java.util.Date;
import java.util.List;

/**
 * 折线图
 */
public class LineView extends View {
    private static final int[] colors = {Color.BLACK, Color.argb(100, 0, 0, 0), Color.TRANSPARENT, 0xFF800080, Color.GREEN, Color.GRAY, 0xFFFF9800, 0xFF90EE90, 0xFFFF7F24, 0xFFFF7F00};

    private GraphicalView mChartView;
    /**
     * 数据集合
     */
    private XYMultipleSeriesDataset mDataset;
    /**
     * 时间序列
     */
    private TimeSeries timeSeries;

    private Context mContext;
    private XYSeries mXYSeries;
    private XYMultipleSeriesRenderer xyMultipleSeriesRenderer;


    /**
     * 最大缓存数
     */
    public int maxCacheNum = 100;


    public LineView(Context context) {
        super(context);
        mContext = context;
    }

   /* public GraphicalView execute() {
        if (mChartView == null) {
            // 时间戳样式
            mChartView = ChartFactory.getTimeChartView(mContext,
                    getTimeSeriesDataset(), getDemoRenderer(), "mm:ss");
        }
        return mChartView;
    }*/

    public GraphicalView execute(String xTitle, String yTitle) {
        if (mChartView == null) {
            // 时间戳样式
        /*    mChartView = ChartFactory.getTimeChartView(mContext,
                    getTimeSeriesDataset(), getDemoRenderer(), "mm:ss");*/
            mDataset = buildDataset();
            double[] xValues = new double[1];
            xValues[0] = 0;
            double[] yValues = new double[1];
            yValues[0] = 0;
            mXYSeries = getXYSeries(mDataset, "", xValues, yValues, 0);
            mDataset.addSeries(mXYSeries);
            XYMultipleSeriesRenderer renderer = getDemoRenderer(xTitle, yTitle);

            //mChartView = ChartFactory.getLineChartView(mContext, mDataset, renderer);
            XYChart chart = new LineChart(mDataset, renderer);
            mChartView = new GraphicalView(mContext, chart);
        }
        return mChartView;
    }

    /**
     * @return 绘制多条曲线调用
     */
    public GraphicalView execute2(String xTitle, String yTitle) {
        if (mChartView == null) {
            // 时间戳样式
        /*    mChartView = ChartFactory.getTimeChartView(mContext,
                    getTimeSeriesDataset(), getDemoRenderer(), "mm:ss");*/
            mDataset = buildDataset();
            double[] xValues = new double[1];
            xValues[0] = 0;
            double[] yValues = new double[1];
            yValues[0] = 0;
            mXYSeries = getXYSeries(mDataset, "", xValues, yValues, 0);
            mDataset.addSeries(mXYSeries);
            xyMultipleSeriesRenderer = getDemoRenderer2(xTitle, yTitle);
            //mChartView = ChartFactory.getLineChartView(mContext, mDataset, xyMultipleSeriesRenderer);
            XYChart chart = new LineChart(mDataset, xyMultipleSeriesRenderer);
            mChartView = new GraphicalView(mContext, chart);
        }
        return mChartView;
    }

    /**
     * 获取时间戳的数据
     *
     * @return
     */
    public XYMultipleSeriesDataset getTimeSeriesDataset() {
        mDataset = new XYMultipleSeriesDataset();
        timeSeries = new TimeSeries("无用标题");
        // 初始化一部分数据
        for (int k = 0; k < maxCacheNum; k++) {
            timeSeries.add(new Date(), 0);
        }
        mDataset.addSeries(timeSeries);
        return mDataset;
    }

    /**
     * 更新视图
     *
     * @param shorts
     */
    public void updateDate(double[] shorts) {
        mDataset.removeSeries(timeSeries);
        timeSeries.clear();
        for (int i = 0; i < shorts.length; i++) {
            timeSeries.add(new Date(), shorts[i]);
        }
        // 在数据集中添加新的点集
        mDataset.addSeries(timeSeries);
        // 曲线更新
        mChartView.invalidate();
    }

    /**
     * 获取渲染器
     *
     * @return
     */
    public XYMultipleSeriesRenderer getDemoRenderer(String xTitle, String yTitle) {
        XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
        mRenderer.setAntialiasing(true);
        // mRenderer.setChartTitle("随机数据");// 标题
        mRenderer.setXTitle(xTitle); // x轴说明
        mRenderer.setYTitle(yTitle);
        mRenderer.setChartTitleTextSize(20);
        mRenderer.setAxisTitleTextSize(30);
        mRenderer.setAxesColor(Color.BLACK);
        mRenderer.setLabelsTextSize(25); // 数轴刻度字体大小
        mRenderer.setLabelsColor(Color.BLACK);
        mRenderer.setLegendTextSize(15); // 曲线说明
        mRenderer.setXLabelsColor(Color.BLACK);
        mRenderer.setYLabelsColor(0, Color.BLACK);
        mRenderer.setShowLegend(false);
        mRenderer.setYLabelsAlign(Paint.Align.LEFT);
        mRenderer.setMargins(new int[]{5, 5, 0, 5});// 上左下右
        XYSeriesRenderer r = new XYSeriesRenderer();
        r.setLineWidth(6);
        r.setColor(Color.BLUE);
        r.setChartValuesTextSize(15);
        r.setChartValuesSpacing(3);
        r.setPointStyle(PointStyle.POINT);
        r.setFillBelowLine(true);
        r.setFillBelowLineColor(Color.WHITE);
        r.setFillPoints(true);
        mRenderer.addSeriesRenderer(r);
        mRenderer.setMarginsColor(Color.WHITE);
        mRenderer.setPanEnabled(false, false);
        mRenderer.setShowGrid(true);
        mRenderer.setYLabels(21);// 设置Y轴默认显示个数
        mRenderer.setXLabels(11);// 设置X轴默认显示个数
        mRenderer.setYAxisMax(10);// 纵坐标最大值
        mRenderer.setYAxisMin(-10);// 纵坐标最小值
        mRenderer.setInScroll(true);
        return mRenderer;
    }

    /**
     * 获取渲染器
     *
     * @return
     */
    public XYMultipleSeriesRenderer getDemoRenderer2(String xTitle, String yTitle) {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        // mRenderer.setChartTitle("随机数据");// 标题
        renderer.setXTitle(xTitle); // x轴说明 //"Frequency(Hz)"
        renderer.setYTitle(yTitle); //"Magnitude(dB)"
        renderer.setChartTitleTextSize(20);
        renderer.setAxisTitleTextSize(30);
        renderer.setAxesColor(Color.BLACK);
        if (BuildConfig.isNewTheme()) {
            renderer.setAxesColor(Color.WHITE);//xy轴线颜色
        }
        int labelsTextSize = 8;
        try {
            int v = 16;//SmartPlayerApplication.getInstance().getResources().getDimensionPixelSize(R.dimen.peq_line_view_label_font_size);
            labelsTextSize = v;
        } catch (Exception e) {
            e.printStackTrace();
        }
        renderer.setLabelsTextSize(labelsTextSize); // 数轴刻度字体大小
        renderer.setLabelsColor(Color.BLACK);
        if (BuildConfig.isNewTheme()) {
            renderer.setLabelsColor(Color.WHITE);
        }
        renderer.setLegendTextSize(20); // 曲线说明
        renderer.setXLabelsColor(Color.BLACK);
        if (BuildConfig.isNewTheme()) {
            renderer.setXLabelsColor(Color.WHITE);
        }
        renderer.setYLabelsColor(0, Color.BLACK);
        if (BuildConfig.isNewTheme()) {
            renderer.setYLabelsColor(0, Color.WHITE);
        }
        renderer.setShowLegend(false);
        renderer.setYLabelsAlign(Paint.Align.RIGHT);
        renderer.setYLabelsPadding(10);
        renderer.setMargins(new int[]{30, 39, -10, 32});// 上左下右
        XYSeriesRenderer r = new XYSeriesRenderer();
        r.setColor(Color.BLACK);
        if (BuildConfig.isNewTheme()) {
            r.setColor(Color.argb(255, 0xFF, 0x73, 0x00));//曲线颜色
        }
        r.setChartValuesTextSize(15);
        r.setChartValuesSpacing(3);
        r.setPointStyle(PointStyle.CIRCLE);
        r.setFillBelowLine(true);
        r.setFillBelowLineColor(Color.WHITE);
        if (BuildConfig.isNewTheme()) {
            r.setFillBelowLineColor(Color.WHITE);
        }
        r.setFillPoints(true);
        renderer.addSeriesRenderer(r);

        renderer.setMarginsColor(Color.WHITE);
        if (BuildConfig.isNewTheme()) {
            renderer.setMarginsColor(Color.TRANSPARENT);//边缘背景颜色
        }
        renderer.setPanEnabled(false, false);

        renderer.setYLabels(26);// 设置Y轴默认显示个数
        renderer.setXLabels(11);// 设置X轴默认显示个数
        renderer.setYAxisMax(13);// 纵坐标最大值
        renderer.setYAxisMin(-13);// 纵坐标最小值
        renderer.setInScroll(false);
        renderer.setZoomEnabled(false, false);
        renderer.setBackgroundColor(Color.argb(255, 0xFF, 0xBC, 0x3C));//背景色
        if (BuildConfig.isNewTheme()) {
            renderer.setBackgroundColor(Color.argb(255, 0x00, 0x00, 0x00));//背景色
        }
        renderer.setApplyBackgroundColor(true);
        renderer.setGridColor(Color.argb(50, 0xff, 0, 0));//网格线颜色
        if (BuildConfig.isNewTheme()) {
            renderer.setGridColor(Color.argb(50, 0xee, 0xee, 0xee));//网格线颜色
        }
        renderer.setShowGridX(false);
        renderer.setShowGridY(true);
        renderer.setGridLineWidth(2);
        renderer.setShowCustomTextGridY(true);
        renderer.setShowLabels(true, true);
        renderer.setShowCustomTextGridX(true);
        return renderer;
    }


    public XYMultipleSeriesDataset buildDataset() {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();/* 创建图表数据集 */
        // addXYSeries(dataset, titles, xValues, yValues, 0);              /* 添加单条曲线数据到图表数据集中 */
        return dataset;
    }

    /*   */

    /**
     * 曲线图(数据集) : 创建曲线图图表数据集
     *
     * @param title   赋予的标题
     * @param xValues x轴的数据
     * @param yValues y轴的数据
     * @return XY轴数据集
     */
    public void updateLine(String title, double[] xValues, double[] yValues) {
        mDataset.removeSeries(mXYSeries);

        mXYSeries = getXYSeries(mDataset, title, xValues, yValues, 0);
        mDataset.addSeries(mXYSeries);
        // 曲线更新
        mChartView.invalidate();
    }

    /**
     * 更新多条曲线
     *
     * @param titles  赋予的标题组,数组大小为线条数
     * @param xValues x轴的数据组,list大小为线条数
     * @param yValues y轴的数据组，list大小为线条数
     */
    public void updateLines(String[] titles, List<double[]> xValues, List<double[]> yValues) {
        mDataset.clear();
        xyMultipleSeriesRenderer.removeAllRenderers();
        int length = titles.length;                         /* 获取标题个数 */
        for (int i = 0; i < length - 1; i++) {
            XYSeriesRenderer r = new XYSeriesRenderer();
            r.setColor(colors[i]);
            r.setChartValuesTextSize(15);
            r.setChartValuesSpacing(3);
            r.setPointStyle(PointStyle.POINT);
            r.setLineWidth(1);
      /*      r.setFillBelowLine(true);
            r.setFillBelowLineColor(Color.WHITE);*/
            r.setFillPoints(true);
            r.setStroke(BasicStroke.DASHED);
            xyMultipleSeriesRenderer.addSeriesRenderer(r);
        }
        XYSeriesRenderer r = new XYSeriesRenderer();
        r.setColor(Color.BLUE);
        r.setChartValuesTextSize(15);
        r.setChartValuesSpacing(3);
        r.setPointStyle(PointStyle.POINT);
        r.setLineWidth(6);
      /*      r.setFillBelowLine(true);
            r.setFillBelowLineColor(Color.WHITE);*/
        r.setFillPoints(true);
        r.setStroke(BasicStroke.SOLID);
        xyMultipleSeriesRenderer.addSeriesRenderer(r);
        addXYSeries(mDataset, titles, xValues, yValues, 0);
        // 曲线更新
        mChartView.invalidate();
    }

    /**
     * @param titles        赋予的标题组,数组大小为线条数
     * @param xValues       x轴的数据组,list大小为线条数
     * @param yValues       y轴的数据组，list大小为线条数
     * @param isSemilogDraw 是否进行半对数绘制
     * @param offsetY
     */
    public void updateLines(String[] titles, List<double[]> xValues, List<double[]> yValues, boolean isSemilogDraw, double offsetY) {
        mDataset.clear();
        xyMultipleSeriesRenderer.removeAllRenderers();
        int length = titles.length;                         /* 获取标题个数 */
        for (int i = 0; i < length; i++) {
            XYSeriesRenderer r = new XYSeriesRenderer();
            r.setColor(colors[i]);
            if (BuildConfig.isNewTheme()) {
                r.setColor(Color.argb(255, 0xFF, 0x73, 0x00));//曲线颜色
            }
            r.setChartValuesTextSize(15);
            r.setChartValuesSpacing(3);
            r.setPointStyle(PointStyle.POINT);
            r.setLineWidth(2);
      /*      r.setFillBelowLine(true);
            r.setFillBelowLineColor(Color.WHITE);*/
            r.setFillPoints(true);
            r.setStroke(/*BasicStroke.DASHED*/BasicStroke.SOLID);
            xyMultipleSeriesRenderer.addSeriesRenderer(r);
        }

        xyMultipleSeriesRenderer.setLabelsColor(Color.RED);

        addXYSeries(mDataset, titles, xValues, yValues, 0); //
        int lineColor = Color.argb(255, 0xf9, 0x71, 0x24);//100,1k,10k 垂直刻度线颜色
        if (BuildConfig.isNewTheme()) {
            lineColor = Color.argb(80, 0xee, 0xee, 0xee);
        }
        // 曲线更新
        if (isSemilogDraw) {
            xyMultipleSeriesRenderer.setXLabels(0);

            double[] xV = xValues.get(0);    //针对xV范围为【20,20000】,按对数进行划分
            /*xyMultipleSeriesRenderer.addXTextLabel(xV[0], "20");*/
            xyMultipleSeriesRenderer.addXTextLabel(xV[24], "");//30
            xyMultipleSeriesRenderer.addXTextLabel(xV[40], "");//40
            xyMultipleSeriesRenderer.addXTextLabel(xV[53], "");//50
            xyMultipleSeriesRenderer.addXTextLabel(xV[64], "");//60
            xyMultipleSeriesRenderer.addXTextLabel(xV[73], "");//70
            xyMultipleSeriesRenderer.addXTextLabel(xV[80], "");//80
            xyMultipleSeriesRenderer.addXTextLabel(xV[87], "");//90
            xyMultipleSeriesRenderer.addXTextLabel(xV[93], "100"/*Math.pow(10,2)*/, new XYMultipleSeriesRenderer.TextLabelGridLineStyle(lineColor, 2));//99.8
            xyMultipleSeriesRenderer.addXTextLabel(xV[133], "");//198
            xyMultipleSeriesRenderer.addXTextLabel(xV[156], "");//296
            xyMultipleSeriesRenderer.addXTextLabel(xV[173], "");//396
            xyMultipleSeriesRenderer.addXTextLabel(xV[186], "");//496
            xyMultipleSeriesRenderer.addXTextLabel(xV[196], "");//590
            xyMultipleSeriesRenderer.addXTextLabel(xV[205], "");//689
            xyMultipleSeriesRenderer.addXTextLabel(xV[213], "");//791
            xyMultipleSeriesRenderer.addXTextLabel(xV[220], "");//893
            /*xyMultipleSeriesRenderer.addXTextLabel(xV[226], "");//990*/
            xyMultipleSeriesRenderer.addXTextLabel(xV[227], "1k"/*Math.pow(10,3)*/, new XYMultipleSeriesRenderer.TextLabelGridLineStyle(lineColor, 2));//1008
            xyMultipleSeriesRenderer.addXTextLabel(xV[267], "");//2011
            xyMultipleSeriesRenderer.addXTextLabel(xV[291], "");//3044
            xyMultipleSeriesRenderer.addXTextLabel(xV[308], "");//4083
            xyMultipleSeriesRenderer.addXTextLabel(xV[321], "");//5111
            xyMultipleSeriesRenderer.addXTextLabel(xV[332], "");//6180
            xyMultipleSeriesRenderer.addXTextLabel(xV[341], "");//7219
            xyMultipleSeriesRenderer.addXTextLabel(xV[349], "");//8289
            xyMultipleSeriesRenderer.addXTextLabel(xV[356], "");//9354
            xyMultipleSeriesRenderer.addXTextLabel(xV[360], "10k"/*Math.pow(10,4)*/, new XYMultipleSeriesRenderer.TextLabelGridLineStyle(lineColor, 2));//10023
            /*xyMultipleSeriesRenderer.addXTextLabel(xV[320], "5k");*/
            //xyMultipleSeriesRenderer.addXTextLabel(xV[347], "8k");
            /*xyMultipleSeriesRenderer.addXTextLabel(xV[360], "10k");*/
            //xyMultipleSeriesRenderer.addXTextLabel(xV[387], "16k");
            //xyMultipleSeriesRenderer.addXTextLabel(xV[xV.length - 1], "20k");
        }
        AbstractChart chart = mChartView.getChart();
        if (chart instanceof XYChart) {
            XYChart xyChart = (XYChart) chart;
            xyChart.setOffsetY((float) offsetY);
        }
        mChartView.invalidate();
    }


    /**
     * 曲线图(被调用方法) : 添加 XY 轴坐标数据 到 XYMultipleSeriesDataset 数据集中
     *
     * @param dataset 最后的 XY 数据集结果, 相当与返回值在参数中
     * @param titles  要赋予的标题
     * @param xValues x轴数据集合
     * @param yValues y轴数据集合
     * @param scale   缩放
     *                <p>
     *                titles 数组个数 与 xValues, yValues 个数相同
     *                tittle 与 一个图标可能有多条曲线, 每个曲线都有一个标题
     *                XYSeries 是曲线图中的 一条曲线, 其中封装了 曲线名称, X轴和Y轴数据
     */
    public void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues, List<double[]> yValues, int scale) {
        if (xValues.size() == 0 || yValues.size() == 0) {
            return;
        }
        int length = titles.length;/* 获取标题个数 */

        for (int i = 0; i < length; i++) {
            XYSeries series = new XYSeries(titles[i], scale); /* 单条曲线数据 */
            double[] xV = xValues.get(i);                     /* 获取该条曲线的x轴坐标数组 */
            double[] yV = yValues.get(i);                     /* 获取该条曲线的y轴坐标数组 */
            int seriesLength = xV.length;
            for (int k = 0; k < seriesLength; k++) {
                series.add(xV[k], yV[k]);                       /* 将该条曲线的 x,y 轴数组存放到 单条曲线数据中 */
            }
            dataset.addSeries(series);                        /* 将单条曲线数据存放到 图表数据集中 */
        }
    }


    public XYSeries getXYSeries(XYMultipleSeriesDataset dataset, String title, double[] xValues, double[] yValues, int scale) {
        XYSeries series = new XYSeries(title, scale); /* 单条曲线数据 */
        /* 获取该条曲线的y轴坐标数组 */
        int seriesLength = xValues.length;
        for (int k = 0; k < seriesLength; k++) {
            series.add(xValues[k], yValues[k]);                       /* 将该条曲线的 x,y 轴数组存放到 单条曲线数据中 */
        }
        // dataset.addSeries(series);                        /* 将单条曲线数据存放到 图表数据集中 */
        return series;
    }

    /**
     * view绘制 配置项
     *
     * @param showGrid             刻度线
     * @param applyBackgroundColor 背景
     * @param showLabels           x,y轴 labels
     * @param showAxes             x,y轴线
     * @param backgroundColor      背景色
     * @param lineColor            频响曲线色
     */
    public void setShow(boolean showGrid, boolean applyBackgroundColor, boolean showLabels, boolean showAxes, int backgroundColor, int lineColor) {
        xyMultipleSeriesRenderer.setShowGrid(showGrid);
        xyMultipleSeriesRenderer.setApplyBackgroundColor(applyBackgroundColor);
        xyMultipleSeriesRenderer.setShowLabels(showLabels);
        xyMultipleSeriesRenderer.setMargins(new int[]{3, 2, 1, 2});// 上左下右
        xyMultipleSeriesRenderer.setShowAxes(showAxes);
        xyMultipleSeriesRenderer.setMarginsColor(backgroundColor);
        xyMultipleSeriesRenderer.setBackgroundColor(/*Color.argb(200, 0xFF, 0xBC, 0x3C)*/backgroundColor);//背景色
        xyMultipleSeriesRenderer.getSeriesRendererAt(0).setColor(lineColor);
        mChartView.invalidate();
    }
}

